package com.yuandian.management.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.management.service.ICodeDetailService;
import com.yuandian.management.utils.Constants;
import com.yuandian.management.utils.JacksonUtil;
import com.yuandian.management.vo.CodeDetailsExcelVo;
import com.yuandian.management.vo.CodeDetailsVo;
import com.yuandian.management.vo.CodeManagementParams;
import com.yuandian.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 返回码详情
 *
 * @author zhoutian
 * @date 2021/1/26 17:13
 */
@RestController
@Api(tags = "返回码管理-返回码详情配置")
@RequestMapping("/outside/code/management/details")
public class CodeDetailsController {

    @Autowired
    private ICodeDetailService codeDetailService;

    /**
     * 列表查询
     *
     * @param params 参数
     * @return PageDataInfo
     */
    @ResponseBody
    @PostMapping("/list")
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "查询条件", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = false, dataType = "Integer"),
	})
    public R getList(@ApiIgnore @RequestBody CodeManagementParams params) {
        Page<CodeDetailsVo> list = codeDetailService.queryList(params);
        return R.ok(list);
    }

    /**
     * 根据ID查询返回码详情信息
     *
     * @param id 数据行id
     * @return Result
     */
    @GetMapping(value = "/getCodeDetails/{id}")
	@ApiOperation(value = "根据id 查询返回码详情信息", notes = "根据id 查询返回码详情信息")
    public R getCodeDetailsById(@PathVariable Integer id) {
        return R.ok(codeDetailService.queryCodeDetailsById(id));
    }

    /**
     * 新增修改保存
     *
     * @param vo 参数
     * @return Result
     */
    @PostMapping("/save")
	@ApiOperation(value = "新增返回码详情", notes = "新增返回码详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "explain", value = "返回码说明", paramType = "body", required = true, dataTypeClass = Integer.class)
	})
    public R save(@Validated @RequestBody CodeDetailsVo vo) {
        return codeDetailService.save(vo);
    }

    /**
     * 删除探针数据过滤信息
     *
     * @param ids id集
     * @return Result
     */
    @DeleteMapping("/{ids}")
    public R deleteByIds(@PathVariable Integer[] ids) {
        return codeDetailService.delByIds(ids);
    }

    /**
     * 上传
     * @param file file
     * @return Result
     * @throws IOException
     */
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) throws IOException {
        Map<String, List<CodeDetailsExcelVo>> resultMap = ExcelUtil.readAndSaveExcelData(file, 0,2, CodeDetailsExcelVo.class, codeDetailService);
        return R.ok("导入完成，成功条数：" + resultMap.get("result").size() + ",失败条数：" + resultMap.get("fail").size());
    }

    /**
     * 返回码配置详情信息模板
     * @param response  response
     * @throws IOException
     */
    @PostMapping("/downloadTemplate")
    public void getTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil.writeExcel(CodeDetailsExcelVo.class, "返回码配置详情信息模板", "返回码配置详情信息模板", new ArrayList<>(), response);
    }
}
